package com.itheima.controller;

import com.itheima.common.Com.User;
import com.itheima.common.pojo.Result;
import com.itheima.common.vo.LoginVo;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("/sendMsg")
    public Result sendMsg(@RequestBody User user) {

        userService.sendMsg(user);
        return Result.success("成功", user.getPhone());

    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        LoginVo login = userService.login(user);
        return Result.success("成功", login);
    }

    @PostMapping("/loginout")
    public Result loginout(HttpServletRequest request) {
        String token = request.getHeader("Authorizationuser");
        log.info("{}",token);

        redisTemplate.delete("token"+token);
        return Result.success("登出成功");
    }

}
